package com.xiaoshuaila.workbench.controller;

import com.xiaoshuaila.settings.domain.user;
import com.xiaoshuaila.settings.service.userservice;
import com.xiaoshuaila.settings.service.userserviceImpl;
import com.xiaoshuaila.tool.DateTimeUtil;
import com.xiaoshuaila.tool.PrintJson;
import com.xiaoshuaila.tool.ServiceFactory;
import com.xiaoshuaila.tool.UUIDUtil;
import com.xiaoshuaila.workbench.domain.Activity;
import com.xiaoshuaila.workbench.domain.Activityremark;
import com.xiaoshuaila.workbench.domain.Clue;
import com.xiaoshuaila.workbench.domain.Tran;
import com.xiaoshuaila.workbench.service.Activityservice;
import com.xiaoshuaila.workbench.service.ActivityserviceImpl;
import com.xiaoshuaila.workbench.service.Clueservice;
import com.xiaoshuaila.workbench.service.ClueserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PipedReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Cluecontroller extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入线索控制器");
        String path=req.getServletPath();
        System.out.println(path);
        Activityservice activityservice=
                (Activityservice) ServiceFactory.getService(new ActivityserviceImpl());
        userservice userservice=
                (userservice) ServiceFactory.getService(new userserviceImpl());
        Clueservice clueservice=
                (Clueservice) ServiceFactory.getService(new ClueserviceImpl());
        if ("/workbench/clue/getuserlist.do".equals(path))
        {
            System.out.println("getuserlist");
            PrintJson.printJsonObj(resp,activityservice.findalluser());
        }
        else if("/workbench/clue/save.do".equals(path))
        {
            System.out.println("save");
            System.out.println(req.getParameter("description"));
            Clue clue=new Clue();
            clue.setId(UUIDUtil.getUUID());
            clue.setFullname(req.getParameter("fullname"));
            clue.setAppellation(req.getParameter("appellation"));
            clue.setOwner(req.getParameter("owner"));
            clue.setCompany(req.getParameter("company"));
            clue.setJob(req.getParameter("job"));
            clue.setEmail(req.getParameter("email"));
            clue.setPhone(req.getParameter("phone"));
            clue.setWebsite(req.getParameter("website"));
            clue.setMphone(req.getParameter("mphone"));
            clue.setState(req.getParameter("state"));
            clue.setSource(req.getParameter("source"));
            clue.setCreateBy(req.getParameter("createBy"));
            clue.setCreateTime(DateTimeUtil.getSysTime());
            clue.setDescription(req.getParameter("description"));
            clue.setContactSummary(req.getParameter("contactSummary"));
            clue.setNextContactTime(req.getParameter("nextContactTime"));
            clue.setAddress(req.getParameter("address"));
            System.out.println(clue);
            if (clueservice.saveclue(clue)==1)
                PrintJson.printJsonFlag(resp,true);
            else PrintJson.printJsonFlag(resp,false);
        }
        else if("/workbench/clue/detail.do".equals(path))
        {
            System.out.println  ("detail");
            Clue clue=clueservice.detail(req.getParameter("id"));
            req.setAttribute("clue",clue);
            req.getRequestDispatcher("/workbench/clue/detail.jsp").forward(req,resp);

        }
        else if("/workbench/clue/getactivity.do".equals(path))
        {
            System.out.println("getactivity");
            PrintJson.printJsonObj(resp,
                    activityservice.findactivity(req.getParameter(
                    "clueid")));
            System.out.println(req.getParameter("clueid"));
        }
        else if("/workbench/clue/pagelist.do".equals(path))
        {
            System.out.println("pagelist");
            Map map=new HashMap();
            map.put("fullname",req.getParameter("name"));
            map.put("company",req.getParameter("company"));
            map.put("phone",req.getParameter("phone"));
            map.put("source",req.getParameter("source"));
            map.put("mphone",req.getParameter("mphone"));
            map.put("owner",req.getParameter("owner"));
            map.put("state",req.getParameter("state"));
            map.put("pageNo",
                    (Integer.valueOf(req.getParameter("pageNo"))-1)*(Integer.valueOf(req.getParameter("pageSize"))));
            map.put("pageSize",Integer.valueOf(req.getParameter(
                    "pageSize")));
            Map map1=clueservice.pageclue(map);
            System.out.println(map1.get("total")+"数量");
            PrintJson.printJsonObj(resp,map1);
        }
        else if("/workbench/clue/deletebund.do".equals(path))
        {
            System.out.println(req.getParameter("id"));
            if (clueservice.deletebund(req.getParameter("id"))==1)
                PrintJson.printJsonFlag(resp,true);
            else PrintJson.printJsonFlag(resp,false);
        }
        else if("/workbench/clue/getactivitylistbynameandnotbyclueid.do".equals(path))
        {
            Map<String,String>map=new HashMap<>();
            map.put("aname",req.getParameter("aname"));
            map.put("clueid",req.getParameter("clueid"));
            PrintJson.printJsonObj(resp,
                    activityservice.findactivitybyname(map));
            System.out.println("getactivitylistbynameandnotbyclueid");

        }
        else if("/workbench/clue/bund.do".equals(path))
        {
            System.out.println("bund");
            Map map=new HashMap();
            map.put("cid",req.getParameter("cid"));
            map.put("aid",req.getParameterValues("aid"));
            PrintJson.printJsonFlag(resp,clueservice.insertcar(map));
        }
        else if("/workbench/clue/getactivitylistbyname.do".equals(path))
        {
            PrintJson.printJsonObj(resp,
                    activityservice.findactivityname(req.getParameter("aname")));
        }
        else if("/workbench/clue/conver.do".equals(path))
        {
            System.out.println("conver");
            String flag=req.getParameter("flag");
            Tran tran=null;
            if (flag!=null)
            {
                tran=new Tran();
                tran.setId(UUIDUtil.getUUID());
                tran.setMoney(req.getParameter("money"));
                tran.setName(req.getParameter("name"));
                tran.setExpectedDate(req.getParameter("expectedDate"));
                tran.setStage(req.getParameter("stage"));
                tran.setActivityId(req.getParameter("activityid"));
                tran.setCreateBy(req.getParameter("createby"));
                tran.setCreateTime(DateTimeUtil.getSysTime());
            }
            if (clueservice.conver(req.getParameter("clueid"),tran,
                    req.getParameter("createby"))){
                System.out.println("成功");
            }
        }
    }
}
